//
// definition of main loading scene
//
var MainLoadingScene = {
    scene:null,
    background:null,
    loadingCount:null,
    loaded:false,
    init:function() {
        this.scene = quicktigame2d.createScene();
        
        var loadingTextures = [
            "graphics/main/kawaztan.png",
            "graphics/main/main_layer0.png",
            "graphics/main/main_layer1.png",
            "graphics/main/text/finish.png",
            "graphics/main/text/go.png",
            "graphics/main/text/ready.png"
        ];
        
        var onloadsprite = (function(self) {
            return function(e) {
                Ti.API.info("onloadsprite: " + e.name);
                if (e.name == "graphics/main/main_background.png") {
                    game.startCurrentScene();
                    self.loaded = true;
                }
            };
        })(this);
        
        var enterframe = (function(self) {
            return function(e) {
            	if (!self.loaded) return;
                if (self.loadingCount >= loadingTextures.length) {
                    game.replaceScene(MainScene.scene);
                    return;
                }
                game.loadTexture(loadingTextures[self.loadingCount]);
                self.loadingCount++;
            };
        })(this);
        
        var activated = (function(self) {
            return function(e) {
                Ti.API.info("main loading scene is activated");

        		self.loadingCount = 0;
        		self.loaded = false;
        
                // Set background color to black
                self.scene.color(0, 0, 0);
                
                // unload previous scene background
                game.unloadTexture('graphics/title/title_background.png');
                
                // Create sprite instance    
                if (self.background == null) {
                    self.background = quicktigame2d.createSprite({image:'graphics/main/main_background.png'});
                }
                
                self.background.alpha = 0.5;
    			// set background at center of the screen
                self.background.x = (game.screen.width   * 0.5) - (self.background.width   * 0.5);
                self.background.y = (game.screen.height  * 0.5) - (self.background.height  * 0.5);
                
                self.scene.add(self.background);
            };
        })(this);
        
        var deactivated = (function(self) {
            return function(e) {
                Ti.API.info("main loading scene is deactivated");
            
                // remove logo from the scene and set variable null
                // that triggers removal from the memory at GC.
                self.scene.remove(self.background);
                self.background = null;
                
                self.loaded = false;
            };
        })(this);
        
        // register scene activationi event
        this.scene.addEventListener('activated',   activated);
        this.scene.addEventListener('deactivated', deactivated);
        this.scene.addEventListener('onloadsprite',  onloadsprite);
        this.scene.addEventListener('enterframe',  enterframe);
    },
};
